package com.frame.work.frame.deal.device0fbe;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.frame.work.common.constant.TypeEnum;
import com.frame.work.common.util.JsonToMqtt;
import com.frame.work.frame.deal.base.BaseFrame;
import com.frame.work.frame.deal.util.DeviceRelationMessageUtil;
import com.frame.work.frame.deal.util.WeakCurrentDeviceUtil;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

import static java.lang.Float.parseFloat;

/**
 * 烟雾传感器0FBE0702
 */
@Data
public class YanwuChuanGanQi_0FBE0702 extends BaseFrame {

    private String switchState;

    public YanwuChuanGanQi_0FBE0702(String gatewayMac, String data) {

        //处理基础数据
        super(gatewayMac, data);
        //设置开关状态
        setSwitchStateParam(data);
        //核心逻辑处理
        CoreLogicDeal(data);
        //这只Action信息
        getActions(data);
        //将数据发送到mqtt队列中
        JsonToMqtt.sendMQTT(gatewayMac, JSONObject.toJSONString(this));
    }


    //**************************************************************************************************************

    /**
     * @Description:设置开关状态
     * @param: data
     * @return: void
     * @author:wjming
     * @Date:2021/1/27 18:58
     */
    private String setSwitchStateParam(String data) {
        String switchState = StringUtils.substring(data, data.length() - 8, data.length() - 6);
        this.setSwitchState(switchState);
        return switchState;
    }

    /**
     * @Description:获取防拆开关信息
     * @param: data
     * @return: void
     * @author:wjming
     * @Date:2021/1/27 18:57
     */
    private static String getFangChaiState(String data) {
        return StringUtils.substring(data, data.length() - 6, data.length() - 4);
    }

    /**
     * @Description:获取电压信息
     * @param: data
     * @return: float
     * @author:wjming
     * @Date:2021/1/28 13:29
     */
    private static float getDianYa(String data) {
        String dianya = StringUtils.substring(data, data.length() - 16, data.length() - 12);
        return parseFloat(StringUtils.substring(dianya, 2, 3) + '.' + StringUtils.substring(dianya, 3, 4) + StringUtils.substring(dianya, 0, 2));
    }

    /**
     * @Description:设置Action信息
     * @param: data
     * @return: java.util.Map<java.lang.String, java.lang.Object>
     * @author:wjming
     * @Date:2021/1/27 19:26
     */
    private Map<String, Object> getActions(String data) {

        Map<String, Object> actions = new HashMap();
        actions.put("dianya", getDianYa(data) + "");
        actions.put("alarmState", this.switchState);
        actions.put("fangChaiState", getFangChaiState(data));
        this.setActions(actions);
        return actions;
    }

    /**
     * @Description:核心逻辑处理
     * @param: data
     * @return: void
     * @author:wjming
     * @Date:2021/1/27 19:30
     */
    private void CoreLogicDeal(String data) {
        String type = "";
        String content = "";
        boolean saveAndSend = false;

        switch (this.getDataType()) {
            //触发上报(报警或解除报警)
            case "00":
                //报警
                type = StringUtils.equals("01", this.switchState) ? TypeEnum.WARNING.code() : TypeEnum.STATE.code();
                content = StringUtils.equals("01", this.getSwitchState()) ? "警报" : "解除警报";
                saveAndSend = true;
                break;
            case "80":
                //防拆开关松动
                List<String> list = new LinkedList<>();
                list.add("alarmState");
                list.add("fangChaiState");
                List<String> deviceActionMessage = DeviceRelationMessageUtil.getDeviceActionParamsValue(this.getDeviceMac(), this.getOdIndex(),
                        this.getDeviceType(), this.getProductType(), list);
                if (CollectionUtils.isEmpty(deviceActionMessage)) {
                    return;
                }
                if (!StringUtils.equals(getFangChaiState(data), deviceActionMessage.get(1))) {
                    type = StringUtils.equals("01", getFangChaiState(data)) ? TypeEnum.NORMAL.code() : TypeEnum.STATE.code();
                    content = StringUtils.equals("01", getFangChaiState(data)) ? "松动" : "恢复正常";
                    saveAndSend = true;
                }
                break;
            default:
                break;
        }
        //判断是否是发送保存
        if (saveAndSend) {
            WeakCurrentDeviceUtil.SaveLogAndSendJpush(this.getDeviceMac(), this.getGatewayMac(), content, type);
        }

    }
}
